package com.tiantianyuegou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.R;
import com.tiantianyuegou.constant.Constant;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.http.SPHelper;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.SelfUtils;
import com.tiantianyuegou.util.Tools;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 重置密码
 */
public class ReSetActivity extends BaseActivity implements View.OnClickListener,TextWatcher {

    private EditText edt_setpwd, edt_confirm_pwd;
    private Button btn_next;
    private LoadingDialog loadingDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resetpwd);
        Tools.changeSystemBar(this);
        setTitle("重置密码");
        initViews();
    }

    @Override
    public void findViewByIds() {
        edt_setpwd = (EditText) findViewById(R.id.edt_setpwd);
        edt_confirm_pwd = (EditText) findViewById(R.id.edt_setpwd_again);

        edt_confirm_pwd.addTextChangedListener(this);
        edt_setpwd.addTextChangedListener(this);
        btn_next = (Button) findViewById(R.id.btn_setpwd_next);
    }

    @Override
    public void initListener() {
        loadingDialog = new LoadingDialog(this);
        btn_next.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    public void setPwd() {

//        if (!NetworkUtils.isNetworkAvailable(this)) {
//            showMsg("请检查网络");
//        }
        String pwd = edt_setpwd.getText().toString().trim();
        String confirmPwd = edt_confirm_pwd.getText().toString().trim();
        SPHelper.setPassword(pwd);
        System.out.println("--------pwd=" + SPHelper.getPassword());

        if (TextUtils.isEmpty(pwd)) {
            showMsg("请输入密码");
            return;
        }
        if(SelfUtils.isConSpeCharacters(pwd)){
            showMsg("密码中不能包含特殊字符");
            return;
        }

        if (TextUtils.isEmpty(confirmPwd)) {
            showMsg("请再次输入密码");
            return;
        }

        if(SelfUtils.isConSpeCharacters(confirmPwd)){
            showMsg("确认密码中不能包含特殊字符");
            return;
        }

        if (!pwd.equals(confirmPwd)) {
            showMsg("两次输入的密码不一致，请重新输入");
            return ;
        }
        final Intent intent = getIntent();
        String code = intent.getStringExtra("vcode");
        String phone = intent.getStringExtra("phone");

        loadingDialog.showDialog();
        loadingDialog.setMsg("正在修改");
        RequestParams params = new RequestParams();
        params.addBodyParameter("vcode", code);
        params.addBodyParameter("phone", phone);
        params.addBodyParameter("password", pwd);
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.FORGET_PASSWORD, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                loadingDialog.dismiss();
                try {

                    JSONObject jsonObject = new JSONObject(responseInfo.result);
                    String code = jsonObject.getString("code");
                    if (code.equalsIgnoreCase("100")) {
                        showMsg("修改成功");
                        Intent intent1=new Intent();
                        intent1.putExtra("type",getIntent().getStringExtra("type"));
                        setResult(Constant.FORGETPWD_RESULT,intent1);
                        finish();
                    } else if (code.equalsIgnoreCase("101")) {
                        showMsg("修改失败");
                    } else if (code.equalsIgnoreCase("103")) {
                        showMsg("验证码错误");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                loadingDialog.dismiss();
                showMsg("当前网络不可用，请检查网络设置");
            }
        });
//        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_setpwd_next:
                setPwd();

                break;
            default:
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        String pwd = edt_setpwd.getText().toString().trim();
        String confirmPwd = edt_confirm_pwd.getText().toString().trim();
        if (pwd!=null&&confirmPwd!=null&&!"".equals(pwd)&&!"".equals(confirmPwd)){
            btn_next.setEnabled(true);
            btn_next.setAlpha(1.0f);
        }else{
            btn_next.setEnabled(false);
            btn_next.setAlpha(0.5f);
        }
    }
}
